import MySQLdb

conn = MySQLdb.connect(host='47.104.31.138', port=3306,
                       user='hellokitty', passwd='Hellokitty.618',
                       db='hrs', charset='utf8')
try:
    with conn.cursor() as cursor:
        affected_rows = cursor.execute(
            'insert into tb_dept values (%s, %s, %s)',
            (90, '研发2部', '天津')
        )
        if affected_rows == 1:
            print('新增部门成功!')
        conn.commit()
except BaseException as ex:
    print(ex)
    conn.rollback()
finally:
    conn.close()
